/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: DriverInformationController
 * Author:   李毅
 * Date:     2019/10/22 16:56
 * Description: 司机信息控制器
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ymtms.applicationsystem.controller;


import com.ymtms.applicationsystem.pojo.DriverInformation;
import com.ymtms.applicationsystem.service.DriverInformationService;
import com.ymtms.applicationsystem.vo.GridRequest;
import com.ymtms.applicationsystem.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈司机信息控制器〉
 *
 * @author 李毅
 * @create 2019/10/22
 * @since 1.0.0
 */
@RestController
@CrossOrigin
public class DriverInformationController {

    @Autowired
    DriverInformationService driverInformationService;

    @RequestMapping("/addDriver")
    public GridResult addDriverInformation(DriverInformation driver){
        GridResult<String> result = new GridResult<>();
        if (driver == null){
            result.setCode(500);
            result.setMsg("请输入有效信息！");
            return result;
        }
        driver.setFlag(0);
        try {
            driverInformationService.insertDriverInformation(driver);
            result.setCode(0);
            result.setMsg("信息插入成功");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/searchDriver")
    public GridResult selectInfo(GridRequest gridRequest, @RequestParam(value = "condition",required = false) String condition){
        GridResult result = new GridResult();
        try {
            List<DriverInformation> list = driverInformationService.findDriverInformationByCondition(condition,gridRequest);
            result.setData(list);
            result.setCount(list.size());
            result.setMsg("查询成功");
            result.setCode(0);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/deleteList")
    public GridResult deleteList(@RequestParam("keys") List<Integer> keys ){
        GridResult result = new GridResult<>();
        if (keys.isEmpty()){
            result.setMsg("数据有误，删除失败");
            result.setCode(500);
            return result;
        }
        try {
            driverInformationService.deleteDriver(keys);
            result.setMsg("删除成功");
            result.setCode(0);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/deleteOneDriver")
    public GridResult deleteOne(@RequestParam("did") Integer did){
        GridResult result = new GridResult<>();
        if (did == 0){
            result.setMsg("数据有误，删除失败");
            result.setCode(500);
            return result;
        }
        try{
            driverInformationService.deleteOne(did);
            result.setMsg("删除成功");
            result.setCode(0);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/updateOneDriver")
    public GridResult delete(@RequestParam("did") Integer did,DriverInformation driverInformation){
        GridResult result = new GridResult();
        //前端数据认证
        System.out.println(driverInformation);
//        System.out.println(driverInformation);
        if(did == 0 || driverInformation ==null){
            result.setMsg("请输入正确数据！");
            result.setCode(500);
            return result;
        }
        try {
            driverInformation.setDiid(did);
            driverInformation.setFlag(0);
            driverInformationService.updateDriver(driverInformation);
            result.setMsg("修改成功");
            result.setCode(0);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

}